<template>
    <div class="search">
        <div class="title search_top">
            <div class="ei">
                <i class="el-icon-edit"></i>
                <h3>筛选搜索</h3>
            </div>
            <div>
                <el-button type="primary" @click="onSearch">查询结果</el-button>
            </div>
        </div>
        <div class="search_input">
            <p>输入搜索:</p>
            <el-input type="text" placeholder="品牌名称/关键字" v-model="name"></el-input>
        </div>
    </div>
    <div class="adddata">
        <div class="title">
            <el-icon>
                <Tickets />
            </el-icon>
            <h3>数据列表</h3>
        </div>

        <div>
            <el-button type="primary" @click="onAddUser">添加</el-button>
        </div>
    </div>
    <div class="tables">
        <!-- 数据的展示 -->
        <el-table align="center" :data="tableData" style="width: 100%" :border="true">
            <el-table-column type="selection"  width="60"> </el-table-column>
            <el-table-column align="center" prop="id" label="编号" width="100" />
            <el-table-column align="center" prop="name" label="品牌名称" width="628" />
            <el-table-column align="center" prop="firstLetter" label="品牌首字母" width="100" />
            <el-table-column align="center" prop="sort" label="排序" width="100" />
            <!-- 自定义列模板 -->
            <el-table-column align="center" label="品牌制造商" width="100">
                <template #default="scope">  
                    <el-switch  :active-value="1" :inactive-value="0" v-model="scope.row.factoryStatus" />
                </template>
            </el-table-column>
            <el-table-column align="center" label="是否启用" width="100">
                <template #default="scope">
                    <el-switch :active-value="1" :inactive-value="0" v-model="scope.row.showStatus" />
                </template>
            </el-table-column>
            <el-table-column align="center" label="相关" width="220" flex="right">
                <template #default="scope">
                    <span>商品：</span>
                     <el-button type="text">100</el-button>
                    <span>评价：</span>
                     <el-button type="text">1000</el-button>
                </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="200" flex="right">
                <template #default="scope">
                    <el-button @click="btn(scope.row.id)">编辑</el-button>
                    <el-button class="del" @click="DelForm(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="pagination">
            <!-- 分页器 -->
            <el-pagination v-model:current-page="pagination.pageNum" v-model:page-size="pagination.pageSize" background
                :page-sizes="[1, 5, 10, 20]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
                @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>
</template>
  
<script setup lang="ts">
import { ref } from 'vue'
import * as api from '../../request/api'
import useTable from '../../hooks/useTables'
import { useRouter , useRoute } from "vue-router"
import { ElMessage, ElMessageBox, ElButton } from 'element-plus'
let name = ref("")
let router = useRouter()
let route = useRoute()

let { tableData, getTableData, pagination, handleSizeChange, handleCurrentChange } = useTable(api.adminA.getProductbrand, {
    // keyword: name.value  // 可以删掉 name.value 不是一个响应式数据
    keyword: name // name 是响应式的
})
console.log(tableData);

// 添加
function onAddUser() {
    console.log(route);
    
    router.push("/pms/addBrand")
}
console.log(onAddUser);


// 编辑
const btn = (id: any) => {
    router.push(
        { path: '/pms/updateBrand', query: { id } }
    )
};
// 删除
let DelForm = (id: number) => {
    ElMessageBox.confirm(
        '是否确认提交!', "提示",
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async (add) => {
            if (add) {
                await api.adminA.adminDeleteForm(id) //删除
            }
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消提交',
            })
        })
}
// 列表数据请求函数调用
getTableData()
let onSearch = async () => {
    console.log(name.value);
    getTableData()

}

</script>
  
<style lang="scss" scoped>
.search {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 20px 0;
    padding: 20px;
    flex-wrap: wrap;
    border: 1px solid #ebeef5;

    .search_top {
        width: 100%;
    }

    .search_input {
        display: flex;
        align-items: center;
        width: 100%;
        padding: 0 40px;
        padding-top: 20px;

        .el-input {
            width: 200px;
            height: 32px;
            font-size: 14px;
            padding: 0 10px;
        }

        p {
            width: 87px;
            height: 32px;
            line-height: 32px;
            font-size: 14px;
            color: #606266;
            padding: 0 12px 0 0;
            box-sizing: border-box;
        }
    }
}

.adddata {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    margin-bottom: 20px;
    border: 1px solid #ebeef5;
}

.title {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .ei {
        display: flex;
        align-items: center;

        h3 {
            color: #303133;
            font-size: 14px;
            margin-left: 10px;
        }
    }

    h3 {
        color: #303133;
        font-size: 14px;
        margin-left: 10px;
    }
}

.tables {
    .pagination {
        margin: 20px 0;
        padding-right: 20px;
        float: right;
    }
}

.del{
    background-color: #f56c6c;
    color: #fff;
}
</style>